﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Buddy.BehaviorTree;
using Buddy.Common;
using Buddy.CommonBot.Settings;
using Buddy.Common.Math;
using Buddy.CommonBot;
using Buddy.Navigation;
using Buddy.Swtor;
using Buddy.Swtor.Objects;

using WingIt.Routines;
using WingIt.Routines.Mirrors;

using Action = Buddy.BehaviorTree.Action;


namespace WingIt
{
    [RevisionControlId("$Id: BWcoreFixup.cs 693 2012-08-13 17:05:06Z Chinajade $")]
    public static partial class WingItCommon
    {
        /// <summary>
        /// <para>"BWcoreFixup" behaviors work around bugs in the BWcore.  These behaviors should be removed as soon as the underlying bug is repaired.</para>
        /// <para>This particular behavior dismounts (if mounted) by starting our rejuvenation spell, then immediately terminating it.</para>
        /// <para>Notes:<list type="bullet">
        /// <item><description><para> * Even when we dismount, BWcore may immediately decide to remount again.</para></description></item>
        /// </list></para>
        /// </summary>
        /// <returns></returns>
        /// 15Jul2012-23:24UTC chinajade
        public static bool BWcoreFixup_Dismount()
        {
            if (!Me.IsMounted) return false;

            StopWithinRangeAndLoS(Me, float.MaxValue);  // stop moving immediately
            if (!Mirror.CooldownTracker.IsOnCooldown(Me.GetMirrorAbility(Me.SelfBuffName())))
            {
                // REVISIT: Mirror.Cast(Me.SelfBuffName(), selfBuffOn => Me, selfBuffWhen => true);
            }

            return true;
        }


        /// <summary>
        /// <para>"BWcoreFixup" behaviors work around bugs in the BWcore.  These behaviors should be removed as soon as the underlying bug is repaired.</para>
        /// <para>This particular behavior delays a Routine's pull until a Companion's harvest is complete.</para>
        /// </summary>
        /// <returns></returns>
        /// 15Jul2012-23:24UTC chinajade
        public static bool BWcoreFixup_WaitForHarvestComplete()
        {
            if (Me.InCombat) return false;
            if (!Me.IsCompanionInUse() || !Me.Companion.IsCasting) return false;

            IntentNotify(_waitForHarvestCompleteThrottle,
                string.Format("Waiting for {0} to complete harvesting", Me.Companion.Name),
                _waitForHarvestCompleteRetryDuration);

            return true;
        }
        private static Throttle _waitForHarvestCompleteThrottle = new Throttle_Timed("CompanionHarvest");
        private static TimeSpan _waitForHarvestCompleteRetryDuration = TimeSpan.FromSeconds(5);
    }
}
